﻿using MaterialDesignThemes.Wpf;

using PLCS.Domain.Menus;
using PLCS.Domain.Roles;
using PLCS.Domain.Users;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;

namespace PLCS.Domain.Data
{
    internal class PLCSDataSeedContributor : IDataSeedContributor, ITransientDependency
    {
        private readonly IRepository<Menu, Guid> _menuRepository;
        private readonly IRepository<Role, Guid> _roleRepository;
        private readonly IRepository<Users.User, Guid> _userRepository;

        public PLCSDataSeedContributor(IRepository<Menu, Guid> menuRepository, IRepository<Role, Guid> roleRepository, IRepository<Users.User, Guid> userRepository)
        {
            _menuRepository = menuRepository;
            _roleRepository = roleRepository;
            _userRepository = userRepository;
        }

        public async Task SeedAsync(DataSeedContext context)
        {
            await CreateMenuAsync();
            await CreateRoleAsync();
            await CreateUserAsync();
        }

        private async Task CreateMenuAsync()
        {
            if (await _menuRepository.GetCountAsync() == 0)
            {
                List<Menu> menus = new List<Menu>();

                Menu productionMenu = new Menu("生产管理", PackIconKind.Reproduction);
                menus.Add(productionMenu);

                Menu orderMenu = new Menu("工单管理", "OrderView", productionMenu.Id);
                menus.Add(orderMenu);

                Menu taskMenu = new Menu("任务管理", "TaskView", productionMenu.Id);
                menus.Add(taskMenu);

                Menu instructionMenu = new Menu("指令管理", "InstructionView", productionMenu.Id);
                menus.Add(instructionMenu);

                Menu userRoleMenu = new Menu("用户权限", PackIconKind.User);
                menus.Add(userRoleMenu);

                Menu userMenu = new Menu("用户管理", "UserView", userRoleMenu.Id);
                menus.Add(userMenu);

                Menu roleMenu = new Menu("权限管理", "RoleView", userRoleMenu.Id);
                menus.Add(roleMenu);

                Menu menuMenu = new Menu("菜单管理", "MenuView", userRoleMenu.Id);
                menus.Add(menuMenu);

                Menu historyMenu = new Menu("历史记录", PackIconKind.History);
                menus.Add(historyMenu);

                Menu hisInstructionMenu = new Menu("历史指令", "HisInstructionView", historyMenu.Id);
                menus.Add(hisInstructionMenu);

                Menu hisTaskMenu = new Menu("历史任务", "HisTaskView", historyMenu.Id);
                menus.Add(hisTaskMenu);

                Menu hisOrderMenu = new Menu("历史工单", "HisOrderView", historyMenu.Id);
                menus.Add(hisOrderMenu);

                Menu psMenu = new Menu("工序方案", PackIconKind.ChatProcessing);
                menus.Add(psMenu);

                Menu processMenu = new Menu("工序管理", "ProcessView", psMenu.Id);
                menus.Add(processMenu);

                Menu solutionMenu = new Menu("方案管理", "SolutionView", psMenu.Id);
                menus.Add(solutionMenu);

                Menu configMenu = new Menu("PLC配置", PackIconKind.Settings);
                menus.Add(configMenu);

                Menu pLCConfigMenu = new Menu("PLC配置", "PLCConfigView", configMenu.Id);
                menus.Add(pLCConfigMenu);

                Menu pLCReadDataMenu = new Menu("PLC字段配置", "PLCReadDataView", configMenu.Id);
                menus.Add(pLCReadDataMenu);

                Menu entityMenu = new Menu("实体管理", PackIconKind.Artstation);
                menus.Add(entityMenu);

                Menu equipmentMenu = new Menu("设备管理", "EquipmentView", entityMenu.Id);
                menus.Add(equipmentMenu);

                Menu stationMenu = new Menu("工位管理", "StationView", entityMenu.Id);
                menus.Add(stationMenu);

                Menu trayMenu = new Menu("托盘管理", "TrayView", entityMenu.Id);
                menus.Add(trayMenu);

                Menu logMenu = new Menu("日志管理", PackIconKind.FileDocumentMultiple);
                menus.Add(logMenu);

                Menu interfaceLogMenu = new Menu("接口日志", "InterfaceLogView", logMenu.Id);
                menus.Add(interfaceLogMenu);

                Menu logShowMenu = new Menu("操作日志", "OperationLogView", logMenu.Id);
                menus.Add(logShowMenu);

                Menu errorLogMenu = new Menu("异常日志", "ErrorLogView", logMenu.Id);
                menus.Add(errorLogMenu);

                await _menuRepository.InsertManyAsync(menus);
            }
        }

        private async Task CreateRoleAsync()
        {
            if (await _roleRepository.GetCountAsync() == 0)
            {
                List<Role> roles = new List<Role>();
                roles.Add(new Role("开发者", "拥有开发者权限", "用户权限，资源管理"));
                await _roleRepository.InsertManyAsync(roles);
            }
        }

        private async Task CreateUserAsync()
        {
            if (await _userRepository.GetCountAsync() == 0)
            {
                List<Users.User> users = new List<Users.User>();
                users.Add(new Users.User("开发者", "admin", "123456", "开发者"));
                users.Add(new Users.User("开发者", "lss", "123456", "开发者"));
                users.Add(new Users.User("开发者", "ysc", "123456", "开发者"));
                await _userRepository.InsertManyAsync(users);
            }
        }
    }
}